package com.e3.basdoc.goods.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.goods.client.GoodsRPCService;
import com.e3.basdoc.goods.model.GoodsModel;
import com.e3.basdoc.goods.model.GoodsQC;
import com.e3.basdoc.measure.model.MeasureUnitModel;
import com.e3.basdoc.measure.server.MeasureUnitServiceClient;
import com.e3.basdoc.tax.model.TaxModel;
import com.e3.basdoc.tax.server.TaxServiceClient;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;

public class GoodsRPCServiceImpl extends GwtRPCAction implements	GoodsRPCService {
		
	public GoodsModel newGoods(final GoodsModel model) throws WebException {
		return new GwtRPCExecutor<GoodsModel>() {
			@Override
			public GoodsModel execute() throws ServiceException, Exception {
				return GoodsServiceClient.getGoodsService().newGoods(model);
			}
		}.doExecute();
	}

	public GoodsModel modifyGoods(final GoodsModel model) throws WebException {
		return new GwtRPCExecutor<GoodsModel>() {
			@Override
			public GoodsModel execute() throws ServiceException, Exception {
				return GoodsServiceClient.getGoodsService().modifyGoods(model);
			}
		}.doExecute();
	}

	public List<GoodsModel> getGoodsByCondition(final GoodsQC goodsQC) throws WebException {
		return new GwtRPCExecutor<List<GoodsModel>>() {
			@Override
			public List<GoodsModel> execute() throws ServiceException, Exception {
				List<GoodsModel> models = GoodsServiceClient.getGoodsQueryService().getGoodsByCondition(goodsQC);
				fillMeasureUnit(models);
				fillTax(models);
				return models;
			}
		}.doExecute();
	}

	public void deleteGoodss(List<GoodsModel> models) throws WebException {
		if(models == null||models.isEmpty()){
			return;
		}
		final List<Long> ids = new ArrayList<Long>(models.size());
		for (GoodsModel goodsModel : models) {
			ids.add(goodsModel.getId());
		}
		
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				GoodsServiceClient.getGoodsService().deleteGoodsByIds(ids);
			}
		}.doExecute();
	}

	public PagingLoadResult<GoodsModel> getPagingGoods(final PagingLoadConfig config)	throws WebException {
		final GoodsQC qc = config.get("LOAD_CONDTION");
		List<GoodsModel> models = new GwtRPCExecutor<List<GoodsModel>>() {
			int start = config.getOffset();
			int max = config.getLimit();
			@Override
			public List<GoodsModel> execute() throws ServiceException,	Exception {
				List<GoodsModel> goods = GoodsServiceClient.getGoodsQueryService().getPagingGoods(qc, start, max);
				if (goods == null || goods.isEmpty()) {
					return goods;
				}
				return goods;
			}
			
		}.doExecute();
		if(models == null||models.isEmpty()){
			return new BasePagingLoadResult<GoodsModel>(new ArrayList<GoodsModel>());
		}
		
		long size = new GwtRPCExecutor<Long>(){
			@Override
			public Long execute() throws ServiceException, Exception {
				return GoodsServiceClient.getGoodsQueryService().getGoodsSizeByCondition(qc);
			}
			
		}.doExecute();
		
		return new BasePagingLoadResult<GoodsModel>(models, config.getOffset(), (int)size);
	}
	
	
	private void fillMeasureUnit(List<GoodsModel> models){
		if(models==null||models.isEmpty()){
			return ;
		}
		List<Long> muIds = new ArrayList<Long>(models.size());
		for (GoodsModel goodsModel : models) {
			Long muId = goodsModel.getMeasureUnitId();
			if(muId==null||muId == 0){
				continue;
			}
			muIds.add(muId);
		}
		
		try {
			List<MeasureUnitModel> muModels = MeasureUnitServiceClient.getMeasureUnitQueryService().getMeasureUnitByIds(muIds);
			if(muModels!=null&&muModels.size()>0){
				Map<Long,MeasureUnitModel> maps = new HashMap<Long, MeasureUnitModel>();
				for (MeasureUnitModel measureUnitModel : muModels) {
					Long key = measureUnitModel.getId();
					maps.put(key, measureUnitModel);
				}
				for (GoodsModel goodsModel : models) {
					Long muId = goodsModel.getMeasureUnitId();
					goodsModel.setMeasureUnit(maps.get(muId));
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	
	private void fillTax(List<GoodsModel> models){
		if(models==null||models.isEmpty()){
			return ;
		}
		List<Long> taxIds = new ArrayList<Long>(models.size());
		for (GoodsModel goodsModel : models) {
			Long muId = goodsModel.getTaxId();
			if(muId==null||muId == 0){
				continue;
			}
			taxIds.add(muId);
		}
		
		try {
			List<TaxModel> taxModels = TaxServiceClient.getTaxQueryService().getTaxByIds(taxIds);
			if(taxModels!=null&&taxModels.size()>0){
				Map<Long,TaxModel> maps = new HashMap<Long, TaxModel>();
				for (TaxModel model : taxModels) {
					Long key = model.getId();
					maps.put(key, model);
				}
				for (GoodsModel goodsModel : models) {
					Long muId = goodsModel.getMeasureUnitId();
					goodsModel.setTax(maps.get(muId));
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	
//	private void fillCust(List<GoodsModel> models){
//		List<Long> taxIds = new ArrayList<Long>(models.size());
//		for (GoodsModel goodsModel : models) {
//			Long muId = goodsModel.getMainSupplierId();
//			if(muId==null||muId == 0){
//				continue;
//			}
//			taxIds.add(muId);
//		}
//		
//		try {
//			List<CustModel> refModels = CustServiceClient.getCustQueryService().getCustModelByIds(ids);
//			if(refModels!=null&&refModels.size()>0){
//				Map<Long,CustModel> maps = new HashMap<Long, CustModel>();
//				for (CustModel model : refModels) {
//					Long key = model.getId();
//					maps.put(key, model);
//				}
//				for (GoodsModel goodsModel : models) {
//					Long muId = goodsModel.getMeasureUnitId();
//					goodsModel.setm(maps.get(muId));
//				}
//			}
//		} catch (ServiceException e) {
//			e.printStackTrace();
//		}
//	
//	}

}
